from flask import Blueprint,json,request,jsonify,get_data
from db import db1
bp_course = Blueprint("course",__name__)
#接口
# @bp_course.route('/couponM',methods = ['POST','GET','PUT','DELETE'])
# def couponM():
#     if request.method =="POST":
#         # 获取用户输入
#         data = request.data
#         if data:  
#             data = json.loads(data)
        
#         else:
#             data = request.form
        
#         #构造sql语句
#         sql = "insert into coupon values(0,'%s',%d,'%s','%s')"%(data['name'],int(data['total']),data['starttime'],data['endtime'])
#         #pymysql
#         db1.insert(sql)
#         #返回结果
#         return 'ok'
#     elif request.method=='GET':
#         id=request.args.get('id')
       
#         if id:
#             #构造sql语句
#             sql="select * from coupon where id=%d"%(int(id))
#             res = db1.find(sql)
#             return jsonify({'code':200,'coupon':res})
#     elif request.method == "PUT":
#         data = request.data
#         if data:
#             data = json.loads(data)
#         else:
#             data = request.form

#         sql="update coupon set name='%s',total=%d,starttime='%s',endtime='%s' where id=%d"%(data['name'],int(data['total']),data['starttime'],data['endtime'],int(data['id']))
#         db1.insert(sql)
#         return jsonify({"code":200})

#     elif request.method == "DELETE":
#         # 获取id
#         id = request.args.get('id')
#         if id:
#             #构造sql语句
#             sql="delete from coupon where id=%d"%(int(id))
#             #调用数据库中insert方法
#             db1.insert(sql)
#             #返回
#             return jsonify({'code':200})


# #部门管理
# @bp_course.route('/deptM',methods=['POST','GET'])
# def deptM():
#     if request.method == "POST":
#         data = get_data()
#         #判断名字是否已经存在
#         sql = "select id from dept where name='%s'"%(data['name'])
#         res = db1.find(sql)
#         #存在直接返回
#         if res:
#             return jsonify({"code":10011,'mes':'已经存在'})
#         #不存在添加
#         else:
#             sql = "insert into dept(name) values('%s')"%(data['name'])
#             db1.insert(sql)
#             return jsonify({"code":200})
#     else:
#         sql = "select * from dept"
#         res = db1.findall(sql)
#         return jsonify({"code":200,'list':res})

# @bp_course.route('/empM',methods=['POST','GET'])
# def empM():
#     if request.method == "POST":
#         data = get_data()
#         #添加员工
#         sql = "insert into emp values(0,'%s',%d,%.2f,%d)"%(data['name'],int(data['sex']),float(data['salery']),int(data['dept_id']))
#         db1.insert(sql)
#         #更新部门表员工总数
#         sql1 = 'select total from dept where id=%d'%(int(data['dept_id']))
#         res = db1.find(sql1)
#         total = int(res['total'])+1
#         sql2 = 'update dept set total=%d where id=%d'%(total,int(data['dept_id']))
#         db1.insert(sql2)
#         return jsonify({"code":200})

from utils.myfile import myfile
import os
# #文件上传
# @bp_course.route('/upload',methods=['POST'])
# def upload():
#     file = request.files['file']
#     cityid = request.form['cityid']
#     #创建文件夹
#     myfile.file_mkdir(cityid)
#     #图片上传
#     file.save(os.path.join('./static/upload/'+str(cityid),file.filename))
#     return jsonify({"code":200,'url':'/static/upload/'+str(cityid)+'/'+file.filename})

# @bp_course.route('/citym')
# def citym():
#     sql = "select id,name from city"
#     res = db1.findall(sql)
#     return jsonify({"code":200,'list':res})

# @bp_course.route('/prom',methods=['POST'])
# def prom():
#    data = get_data()
#    sql = "insert into product(title,price,city_id,pic) values('%s',%.2f,%d,'%s')"%(data['title'],float(data['price']),int(data['city_id']),data['pic'])
#    db1.insert(sql)
#    return jsonify({"code":200})

# sort = request.args.get('sort')
# if cityid == '0':
#     sql = "select * from product order by price %s limit %d,%d"%(sort,start,page_size)
#     sql1 - "select count(id) from product"
#     res = db1.find(sql1)
#     todal = res['count(id)']
# else:
    

# else:
# page = int(request.args.get('page'))

# @bp_course.route('/tlogin',methods=['POST'])
# def tlogin():
#     data = 

@bp_course.route('/blogin',methods=['POST'])
def blogin():
    # 获取用户输入
    # 判断用户名密码是否为空
    # 根据用户名去数据库查
    # 查看到比较比较密码
    # 比对成功返回userid username
    # 失败提示密码错误
    # 没查到返回此用户不存在
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    
    try:
        username = data['username']
        password = data['password']
    except:
        return jsonify({"code":10010,'mes':'必须输入用户名和密码'})
    
    if not all([username,password]):
        return jsonify({"code":10010,'mes':'必须输入用户名和密码'})
    #根据用户名去数据库查
    sql = "select * from author where username='%s'"%(username)
    mes = db1.find(sql)
    if mes:
        #查看到比较密码
        if mes['password'] == password:
            #比对成功返回userid username
            return jsonify({"code":200,'mes':'登录成功','userid':mes['id'],'username':mes['username']})
        #失败提示密码错误
        else:
             return jsonify({"code":10012,'mes':'密码错误'})
    else:
        #没查到返回此用户不存在
        return jsonify({"code":10011,'mes':'用户不存在'})